<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * This is a sample module for PyroCMS
 *
 * @author 		Jerel Unruh - PyroCMS Dev Team
 * @website		http://unruhdesigns.com
 * @package 	PyroCMS
 * @subpackage 	Sample Module
 */
class Admin extends Admin_Controller
{
	protected $section = 'packages';
	public function __construct()
	{
		parent::__construct();
		// Load all the required classes
		$this->load->library('form_validation');
		
		$this->load->model('uploking_packages_m');
		$this->lang->load('uploking_admin');
		// Set the validation rules
		$this->validation_rules = array(
			array(
				'field' => 'name',
				'label' => 'lang:uploking_package_name_title',
				'rules' => 'trim|max_length[255]|min_length[2]|alpha_numeric|required|callback__check_code'
			),
			array(
				'field' => 'price',
				'label' => 'lang:uploking_package_price_title',
				'rules' => 'trim|max_length[8]|numeric|required'
			),
			array(
				'field' => 'month',
				'label' => 'lang:uploking_package_month_title',
				'rules' => 'trim|max_length[8]|integer|required'
			),
			array(
				'field'	=> 'description',
				'label'	=> 'lang:uploking_package_description_title'
			)
		);

		$this->template->set_partial('shortcuts', 'admin/partials/shortcuts');
	}

	/**
	 * List all items
	 */
	public function index()
	{
		//set the base/default where clause
		$base_where = array();
		// Create pagination links
		$total_rows = $this->uploking_packages_m->count_by($base_where);
		$pagination = create_pagination('admin/package/index', $total_rows);

		// Using this data, get the relevant results
		$packages = $this->uploking_packages_m->limit($pagination['limit'])->get_many_by($base_where);

		//do we need to unset the layout because the request is ajax?
		$this->input->is_ajax_request() ? $this->template->set_layout(FALSE) : '';

		$this->template
			->title($this->module_details['name'])
			->append_metadata(js('admin/filter.js'))
			->set('pagination', $pagination)
			->set('packages', $packages);

		$this->input->is_ajax_request() ? $this->template->build('admin/tables/posts', $this->data) : $this->template->build('admin/index', $this->data);
			
	}
	
	public function create()
	{
		$this->form_validation->set_rules($this->validation_rules);
		if ($this->form_validation->run())
		{
			$id = $this->uploking_packages_m->insert(array(
				'name'			=> $this->input->post('name'),
				'price'			=> $this->input->post('price'),
				'month'			=> $this->input->post('month'),
				'description'		=> $this->input->post('description'),
				'created_time'	=> time()
			));
			if ($id)
			{
				$this->session->set_flashdata('success', sprintf($this->lang->line('uploking_package_add_success_label'), $this->input->post('name')));
			}
			else
			{
				$this->session->set_flashdata('error', $this->lang->line('uploking_package_add_error_label'));
			}
			$this->input->post('btnAction') == 'save_exit' ? redirect('admin/package') : redirect('admin/package/edit/' . $id);
		}
		else
		{
			// Go through all the known fields and get the post values
			foreach ($this->validation_rules as $key => $field)
			{
				$package->$field['field'] = set_value($field['field']);
			}
		}

		$this->template
			->title($this->module_details['name'], lang('stoepje_voucher_create_title'))
			->append_metadata($this->load->view('fragments/wysiwyg', $this->data, TRUE))
			->append_metadata(css('jquery/jquery.tagsinput.css'))
			->set('package', $package)
			->build('admin/form');
	}
	public function edit($id = 0)
	{
		$id OR redirect('admin/package');
		$package = $this->uploking_packages_m->get($id);
		$package OR redirect('admin/package');
		$this->form_validation->set_rules(array_merge($this->validation_rules, array(
			'name' => array(
				'field' => 'name',
				'label' => 'lang:uploking_package_name_title',
				'rules' => 'trim|max_length[255]|min_length[2]||required|callback__check_code['.$id.']'
			)
		)));
	
		if ($this->form_validation->run())
		{
	
			$result = $this->uploking_packages_m->update($id, array(
				'name'			=> $this->input->post('name'),
				'price'			=> $this->input->post('price'),
				'month'		=> $this->input->post('month'),
				'description'		=> $this->input->post('description')
			));
				
			if ($result){
				$this->session->set_flashdata(array('success' => sprintf(lang('uploking_package_edit_success_label'), $this->input->post('name'))));
			}else{
				$this->session->set_flashdata('error', $this->lang->line('uploking_package_edit_error_label'));
			}
	
			// Redirect back to the form or main page
			$this->input->post('btnAction') == 'save_exit' ? redirect('admin/package') : redirect('admin/package/edit/' . $id);
		}
	
		// Go through all the known fields and get the post values
		foreach ($this->validation_rules as $key => $field)
		{
			if (isset($_POST[$field['field']]))
			{
				$package->$field['field'] = set_value($field['field']);
			}
		}
		$this->template
		->title($this->module_details['name'], sprintf(lang('uploking_package_edit_title'), $package->name))
		->append_metadata($this->load->view('fragments/wysiwyg', $this->data, TRUE))
		->set('package', $package)
		->build('admin/form');
	}
	public function delete($id = 0)
	{
		// Delete one
		$ids = ($id) ? array($id) : $this->input->post('action_to');
	
		// Go through the array of slugs to delete
		if ( ! empty($ids))
		{
			$package_names = array();
			foreach ($ids as $id)
			{
				// Get the current page so we can grab the id too
				if ($package = $this->uploking_packages_m->get($id))
				{
					$this->uploking_packages_m->delete($id);
					$package_names[] = $package->name;
				}
			}
		}
	
		// Some pages have been deleted
		if ( ! empty($package_names))
		{
			// Only deleting one page
			if (count($package_names) == 1)
			{
				$this->session->set_flashdata('success', sprintf($this->lang->line('uploking_package_delete_success_label'), $package_names[0]));
			}
			// Deleting multiple pages
			else
			{
				$this->session->set_flashdata('success', sprintf($this->lang->line('uploking_package_delete_mass_success_label'), implode('", "', $package_names)));
			}
		}
		// For some reason, none of them were deleted
		else
		{
			$this->session->set_flashdata('notice', lang('uploking_package_delete_error_label'));
		}
	
		redirect('admin/package');
	}
	public function action()
	{
		switch ($this->input->post('btnAction'))
		{			
			case 'delete':
				$this->delete();
				break;
			default:
				redirect('admin/package');
			break;
		}
	}
	public function _check_code($code = '', $id = 0){
		$this->form_validation->set_message('_check_code', sprintf(lang('uploking_package_existed_error_label'), $code));
		if($id){
			$this->uploking_packages_m->where('id !=', $id);
		}
		if($this->uploking_packages_m->get_by('name',$code)){
			return false;
		}else{
			return true;
		}
	}
}
